package com.ksm.calcul;

import java.util.ArrayList;
import java.util.List;

public class CalculChargesSociales {

	private class BaremeChargesSociales {

		private String cotisations;
		private Double taux;

		public String getCotisations() {
			return cotisations;
		}

		public void setCotisations(String cotisations) {
			this.cotisations = cotisations;
		}

		public Double getTaux() {
			return taux;
		}

		public void setTaux(Double taux) {
			this.taux = taux;
		}

		public BaremeChargesSociales(String cotisations, Double taux) {
			super();
			this.cotisations = cotisations;
			this.taux = taux / 100;
		}

	}

	private List<BaremeEchelon> baremesRetraiteComplemetaire = new ArrayList<BaremeEchelon>();
	BaremeChargesSociales baremeMaladie;
	BaremeChargesSociales baremeIndemniteJournalieres;
	BaremeChargesSociales baremeRetraiteBase;
	BaremeChargesSociales baremeInvaliditeDeces;
	BaremeChargesSociales baremeAllocationFamiliale;
	BaremeChargesSociales baremeCsgCrds;

	private Double tauxForfaitaireCS = 40.0;
	private Double pass = 37032.0; // plafond annuel securite sociale

	public CalculChargesSociales() {

		baremeMaladie = new BaremeChargesSociales("MALADIE", 6.5);
		baremeIndemniteJournalieres = new BaremeChargesSociales("INDEMNITES_JOURNALIERES", 0.7);
		baremeRetraiteBase = new BaremeChargesSociales("RETRAITE_BASE", 16.85);
		baremeInvaliditeDeces = new BaremeChargesSociales("INVALIDITE_DECES", 1.6);
		baremeAllocationFamiliale = new BaremeChargesSociales("ALLOCATIONS_FAMILIALES", 5.4);
		baremeCsgCrds = new BaremeChargesSociales("CSG_CRDS", 8.0);

		baremesRetraiteComplemetaire.add(new BaremeEchelon(7.0, 0, pass.intValue()));
		baremesRetraiteComplemetaire.add(new BaremeEchelon(8.0, pass.intValue(), 4 * pass.intValue()));
	}

	public Double calcul(Double revenu, Double cotisationSociales) {

		Double chargesSociales = 0.0;
		Double assietteCSGCRDS = 0.0;
		Double assietteMaladie = 0.0;
		Double assietteIndemniteJournalieres = 0.0;
		Double assietteRetraiteBase = 0.0;
		Double assietteInvaliditeDeces = 0.0;
		Double assietteAllocationFamiliale = 0.0;

		if (revenu != null) {
			if (cotisationSociales == null || cotisationSociales == 0) {
				cotisationSociales = tauxForfaitaireCS / 100 * revenu;
			}
			assietteCSGCRDS = revenu + cotisationSociales;
			assietteMaladie = revenu;
			assietteIndemniteJournalieres = revenu;
			assietteRetraiteBase = pass;

			assietteInvaliditeDeces = pass;
			assietteAllocationFamiliale = revenu;

			chargesSociales = chargesSociales + (assietteMaladie * baremeMaladie.getTaux());
			chargesSociales = chargesSociales + (assietteIndemniteJournalieres * baremeIndemniteJournalieres.getTaux());
			chargesSociales = chargesSociales + (assietteRetraiteBase * baremeRetraiteBase.getTaux());

			chargesSociales = chargesSociales + calculRetraiteComplementaire(revenu);

			chargesSociales = chargesSociales + (assietteInvaliditeDeces * baremeInvaliditeDeces.getTaux());
			chargesSociales = chargesSociales + (assietteAllocationFamiliale * baremeAllocationFamiliale.getTaux());
			chargesSociales = chargesSociales + (assietteCSGCRDS * baremeCsgCrds.getTaux());

		}

		return chargesSociales;
	}

	public Double calculRetraiteComplementaire(Double revenu) {

		Double charges = 0.0;
		if (revenu != null) {

			for (BaremeEchelon bareme : baremesRetraiteComplemetaire) {
				if (revenu > bareme.getMontantMin()) {
					if (revenu > bareme.getMontantMax()) {
						charges = charges + ((bareme.getMontantMax() - bareme.getMontantMin()) * bareme.getTaux());
					} else {
						charges = charges + ((revenu - bareme.getMontantMin()) * bareme.getTaux());
					}
				}
			}
		}

		return charges;
	}
}
